package com.panfeng.xcloud.boss.provider.assets.service.impl;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.*;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.RebateRecordMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dto.RebateRecordExtendItemDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.request.QueryRebateRecordPageReqDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.request.QueryUserInviterCountReqDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.request.RebateDetailReqDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.response.QueryRebateRecordPageRespDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.response.RebateDetailRespDTO;
import com.panfeng.xcloud.boss.provider.assets.service.IRebateRecordService;
import com.panfeng.xcloud.common.core.enums.DeleteStatusEnum;
import com.panfeng.xcloud.common.core.enums.GetRebateStatusEnum;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.dao.assets.entity.CoinList;
import com.panfeng.xcloud.dao.assets.entity.RebateRecord;
import com.panfeng.xcloud.dao.assets.entity.UserWalletInfo;
import com.panfeng.xcloud.dao.base.MapperSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RebateRecordServiceImpl extends MapperSupport<Integer, RebateRecord, RebateRecordMapperExt> implements IRebateRecordService {

    @Autowired
    private RebateRecordMapperExt rebateRecordMapperExt;

    @Autowired
    private CoinListMapperExt coinListMapperExt;

    @Autowired
    private UserInviterMapperExt userInviterMapperExt;

    @Autowired
    private UserWalletInfoMapperExt userWalletInfoMapperExt;

    /**
     * 查询相关返佣记录
     *
     * @param queryRebateRecordPageReqDTO
     * @return
     */
    @Override
    public PageResponse<QueryRebateRecordPageRespDTO> queryRebateRecord(QueryRebateRecordPageReqDTO queryRebateRecordPageReqDTO) {
        PageResponse<RebateRecordExtendItemDTO> pageResponse = super.selectPage(queryRebateRecordPageReqDTO, rebateRecordMapperExt::queryRebateRecordExtPageCount, rebateRecordMapperExt::queryRebateRecordExtPage);

        List<RebateRecordExtendItemDTO> items = pageResponse.getItems();
        log.info(">>>> queryRebateRecord-items.list：{} <<<", (null != items ? JSON.toJSONString(items) : ""));
        List<QueryRebateRecordPageRespDTO> queryRebateRecordRespDTOS = ConvertUtils.convert(items, QueryRebateRecordPageRespDTO.class);

        PageResponse<QueryRebateRecordPageRespDTO> pageFinalResponse = new PageResponse();
        pageFinalResponse.setTotalCount(pageResponse.getTotalCount());
        pageFinalResponse.measureTotalPage(pageResponse.getTotalCount(), pageResponse.getPageSize());
        pageFinalResponse.setItems(queryRebateRecordRespDTOS);
        pageFinalResponse.setPageNumber(pageResponse.getPageNumber());
        pageFinalResponse.setPageSize(pageResponse.getPageSize());

        return pageFinalResponse;
    }

    /**
     * 查询相关返佣信息
     *
     * @param rebateDetailReqDTO
     * @return
     */
    public RebateDetailRespDTO rebateDetail(RebateDetailReqDTO rebateDetailReqDTO) {
        QueryRebateRecordPageReqDTO convert = ConvertUtils.convert(rebateDetailReqDTO, QueryRebateRecordPageReqDTO.class);
        List<RebateRecordExtendItemDTO> rebateRecordExtendItemDTOS = rebateRecordMapperExt.queryRebateRecordExtPage(convert);

        BigDecimal totalRebateAmount = BigDecimal.ZERO;
        BigDecimal settledRebateAmount = BigDecimal.ZERO;
        BigDecimal unSettledRebateAmount = BigDecimal.ZERO;
        for (RebateRecordExtendItemDTO rebateRecordExtendItemDTO : rebateRecordExtendItemDTOS) {
            if (rebateRecordExtendItemDTO.getDataStatus() == DeleteStatusEnum.NORMAL.value) {
                totalRebateAmount = totalRebateAmount.add(rebateRecordExtendItemDTO.getRebateAmount());
                if (rebateRecordExtendItemDTO.getGetRebate() == GetRebateStatusEnum.SETTLED.value) {
                    settledRebateAmount = settledRebateAmount.add(rebateRecordExtendItemDTO.getRebateAmount());
                } else if (rebateRecordExtendItemDTO.getGetRebate() == GetRebateStatusEnum.UNSETTLED.value) {
                    unSettledRebateAmount = unSettledRebateAmount.add(rebateRecordExtendItemDTO.getRebateAmount());
                }
            }
        }

        Calendar calendarToday = Calendar.getInstance();
        calendarToday.set(Calendar.HOUR_OF_DAY, 0);
        calendarToday.set(Calendar.MINUTE, 0);
        calendarToday.set(Calendar.SECOND, 0);
        calendarToday.set(Calendar.MILLISECOND, 0);

        Calendar calendarWeek = Calendar.getInstance();
        calendarWeek.add(Calendar.DAY_OF_YEAR, -7);
        calendarWeek.set(Calendar.HOUR_OF_DAY, 0);
        calendarWeek.set(Calendar.MINUTE, 0);
        calendarWeek.set(Calendar.SECOND, 0);
        calendarWeek.set(Calendar.MILLISECOND, 0);

        Calendar calendarMonth = Calendar.getInstance();
        calendarMonth.add(Calendar.DAY_OF_YEAR, -30);
        calendarMonth.set(Calendar.HOUR_OF_DAY, 0);
        calendarMonth.set(Calendar.MINUTE, 0);
        calendarMonth.set(Calendar.SECOND, 0);
        calendarMonth.set(Calendar.MILLISECOND, 0);

        Date currentDate = new Date();
        QueryUserInviterCountReqDTO queryUserInviterCountReqDTO = new QueryUserInviterCountReqDTO();
        queryUserInviterCountReqDTO.setInviterId(rebateDetailReqDTO.getUserId());
        queryUserInviterCountReqDTO.setStatus(0);
        BigDecimal totalInviteAmount = new BigDecimal(userInviterMapperExt.queryUserInviterCount(queryUserInviterCountReqDTO));

        queryUserInviterCountReqDTO.setCreateTimeStart(calendarToday.getTime());
        queryUserInviterCountReqDTO.setCreateTimeEnd(currentDate);
        BigDecimal todayInviteAmount = new BigDecimal(userInviterMapperExt.queryUserInviterCount(queryUserInviterCountReqDTO));

        queryUserInviterCountReqDTO.setCreateTimeStart(calendarWeek.getTime());
        queryUserInviterCountReqDTO.setCreateTimeEnd(currentDate);
        BigDecimal weekInviteAmount = new BigDecimal(userInviterMapperExt.queryUserInviterCount(queryUserInviterCountReqDTO));

        queryUserInviterCountReqDTO.setCreateTimeStart(calendarMonth.getTime());
        queryUserInviterCountReqDTO.setCreateTimeEnd(currentDate);
        BigDecimal monthInviteAmount = new BigDecimal(userInviterMapperExt.queryUserInviterCount(queryUserInviterCountReqDTO));

        UserWalletInfo req = new UserWalletInfo();
        req.setUserId(rebateDetailReqDTO.getUserId());
        UserWalletInfo userWalletInfo = userWalletInfoMapperExt.selectOne(req);

        RebateDetailRespDTO rebateDetailRespDTO = new RebateDetailRespDTO();
        rebateDetailRespDTO.setTotalInviteAmount(totalInviteAmount);
        rebateDetailRespDTO.setTodayInviteAmount(todayInviteAmount);
        rebateDetailRespDTO.setWeekInviteAmount(weekInviteAmount);
        rebateDetailRespDTO.setMonthInviteAmount(monthInviteAmount);
        rebateDetailRespDTO.setTotalRebateAmount(totalRebateAmount);
        rebateDetailRespDTO.setSettledRebateAmount(settledRebateAmount);
        rebateDetailRespDTO.setUnSettledRebateAmount(unSettledRebateAmount);
        rebateDetailRespDTO.setSubTotalProfit(userWalletInfo.getSubUserTotalProfit());

        return rebateDetailRespDTO;
    }

}
